﻿using System;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Security.Permissions;

namespace LeanEngine.Business.Security
{
    /// <summary>
    /// Provides functionalities to add behaviors to evaluate whether a user has or not access to a method.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
    public sealed class SecureContextAttribute : ContextAttribute
    {
        /// <summary>
        /// Initializes a new instance of LeanEngine.Business.Security.SecureContextAttribute class.
        /// </summary>
        public SecureContextAttribute() : base("SecureContextAttribute") { }

        /// <summary>
        /// Adds the current context property to the given message.
        /// </summary>
        /// <param name="ctorMsg">The System.Runtime.Remoting.Activation.IConstructionCallMessage to which to add the context property.</param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
        {
            var securityProperty = new SecurityContextProperty();
            ctorMsg.ContextProperties.Add(securityProperty);
        }

        /// <summary>
        /// Returns a Boolean value indicating whether the context parameter meets the context attribute's requirements.
        /// </summary>
        /// <param name="ctx">The context in which to check.</param>
        /// <param name="ctorMsg">The System.Runtime.Remoting.Activation.IConstructionCallMessage to which to add the context property.</param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        public override bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg)
        {
            return false;
        }
    }
}
